package com.colin.aiyi.presenter.Imp_de;

import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.dao.ImpxUtils3_de.IService;
import com.colin.aiyi.dao.ImpxUtils3_de.ImpYuLu;
import com.colin.aiyi.dao.ImpxUtils3_de.ImpOther;
import com.colin.aiyi.entity.DataEntity;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.entity.response.YiJuAndYiShuoResponse;
import com.colin.aiyi.ui.Home.yulu.IViewYuLu;
import com.colin.aiyi.presenter.Interface_Derecated.AbsPresenter;
import com.colin.aiyi.view.ShareBottomDialog;

import java.util.List;

/**
 * Created by hl on 2017/5/26.
 */
@Deprecated
public class ImlYuLuP extends AbsPresenter<YiJuAndYiShuoResponse> {
    IService<YiJuAndYiShuoResponse> iService=new ImpYuLu();
    IViewYuLu iViewYuLu;
    private int totalCount;
    private boolean pullMore=false;
    private String  contentType=Constant.TAB_TYPE_LIST;
    private int pageNum=1;
    private String lastType="";  //上次类型

    public ImlYuLuP(IViewYuLu iViewYuLu) {
        this.iViewYuLu = iViewYuLu;
    }

    @Override
    public void onSuccess(Object t) {
        dataPreprocess((YiJuAndYiShuoResponse) t);
    }

    private void dataPreprocess(YiJuAndYiShuoResponse d){
        DataEntity dataEntity = d.getData();
        totalCount=dataEntity.getCount();
        List<YiJuAndYiShuoEntity> list = dataEntity.getItems();
        if (!lastType.equals(contentType))
        {
            pullMore=false;
        }
        lastType=contentType;
        if (pullMore)
        {
            iViewYuLu.pullMoreComlite();
        }else {
            iViewYuLu.getData().clear();
            iViewYuLu.refreshComplite();
        }
        if (list!=null&list.size()>0)
        {
            iViewYuLu.getData().addAll(list);
        }
    }



    /********
     * 获取一说列表
     * @param //pageNum  目标页面
     * @param //cType  类型
     */

    @Override
    public void getAllT(String... type) {
        this.contentType=type[type.length-1];
        iService.getAllT(this,pageNum+"",contentType);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId())
        {
            case R.id.text_share:
                ((TextView)view).getCompoundDrawables()[0].setLevel(1);
                ShareBottomDialog dialog = new ShareBottomDialog();
                dialog.show(iViewYuLu.getFM());
                break;
            case R.id.text_like:
                int a= ((TextView)view).getCompoundDrawables()[0].getLevel();
                int sum= Integer.parseInt(((TextView) view).getText().toString());
                sum++;
                a++;
                boolean operType=true;
                if (a==2)
                {
                    sum-=2;
                    a-=2;
                    operType=false;
                }
                ((TextView) view).setText(sum+"");
                ((TextView)view).getCompoundDrawables()[0].setLevel(a);
                ImpOther.getInstance().doLike(operType,iViewYuLu.getData().get(position).getId()+"","2");
                break;
        }
    }


    public void onRefresh() {
        pullMore=false;
        pageNum=1;
        getAllT(pageNum+"",contentType);
    }

    @Override
    public void onLoadMoreRequested() {
        if (iViewYuLu.getData().size() < totalCount) {
            pullMore=true;
            pageNum++;
            getAllT(pageNum+"",contentType);
        } else {
            iViewYuLu.pullMoreNoMore();
        }
    }
}
